package com.jc.tools;

import android.content.Context;
import android.provider.Settings;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import link.zhidou.utils.NetworkHelper;

/**
 * Created by jc on 17-4-24.
 */

public class Util {

    public volatile static Context sContext;

    public static String getDevId() {
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileReader = new FileReader("/sys/block/mmcblk0/device/cid");
            bufferedReader = new BufferedReader(fileReader);
            String id = bufferedReader.readLine();
            return id;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String id = SysProp.get("persist.sys.zd.dev_id","");
        if(id.equals("")) {
            id = UUID.randomUUID().toString();
            try {
                SysProp.set("persist.sys.zd.dev_id", id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return id;
    }

    public static String getAndroidId(Context context) {
        if (context == null) {
            context = sContext;
        }
        return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    public static void init(Context context) {
        sContext = context;
    }

    public static String getFileMD5(File file) throws NoSuchAlgorithmException, IOException {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest;
        FileInputStream in = null;
        try {

            byte buffer[] = new byte[1024];
            int len;
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }

    public static void calculateMd5(final File file) {
        Flowable.create(new FlowableOnSubscribe<String>() {
            @Override
            public void subscribe(FlowableEmitter<String> e) throws Exception {
                String md5 = getFileMD5(file);
                e.onNext(md5);
            }
        }, BackpressureStrategy.BUFFER).subscribeOn(Schedulers.io()).subscribe(new Consumer<String>() {
            @Override
            public void accept(String md5) throws Exception {
                Log.d("Util", "md5: " + md5);
            }
        });
    }

    public boolean isCleartextTrafficPermitted() {
        return NetworkHelper.isCleartextTrafficPermitted();
    }
}
